package com.qeegoo.b2bautozityp.moudle.good.viewmodel.carmodel;

import android.databinding.ObservableField;
import android.support.v4.app.Fragment;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.BR;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.base.BaseAdapter;
import com.qeegoo.b2bautozityp.moudle.good.model.CarModelBean;
import com.qeegoo.b2bautozityp.moudle.good.model.CarYearBean;

import java.util.ArrayList;



/**
 * 王少岩 在 2017/9/25 创建了它
 */

public class CarModelTwoViewModel {
    public ObservableField<String> title = new ObservableField<>("");
    private RequestApi mRequestApi;
    private Fragment mFragment;
    private BaseAdapter<CarModelBeanViewModel> mAdapterBean;
    private BaseAdapter<CarModelYearViewModel> mAdapterYear;
    private ArrayList<CarModelYearViewModel> years = new ArrayList<>();
    private ArrayList<CarModelYearViewModel> capacitys = new ArrayList<>();
    private ArrayList<CarModelYearViewModel> yearOrCapacity = new ArrayList<>();
    private ArrayList<CarModelBeanViewModel> carmodels = new ArrayList<>();

    public CarModelTwoViewModel(Fragment fragment, RequestApi requestApi) {
        mFragment = fragment;
        mRequestApi = requestApi;
        mAdapterBean = new BaseAdapter<>(BR.viewModel, R.layout.adapter_goods_carmodel_item_bean, carmodels);
        mAdapterYear = new BaseAdapter<>(BR.viewModel, R.layout.adapter_goods_carmodel_year, yearOrCapacity);
    }

    public BaseAdapter<CarModelYearViewModel> getAdapterYear() {
        return mAdapterYear;
    }

    public BaseAdapter<CarModelBeanViewModel> getAdapterBean() {
        return mAdapterBean;
    }

    public void getCarYear(String seriesIds) {
        mRequestApi.listCarYear(HttpParams.getListCarYear(seriesIds))
                .compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribe(new ProgressSubscriber<CarYearBean>() {
                    @Override
                    public void onNext(CarYearBean carYearBean) {
                        for (String year : carYearBean.caryears) {
                            years.add(new CarModelYearViewModel(year));
                        }
                        for (String capacity : carYearBean.capacitys) {
                            capacitys.add(new CarModelYearViewModel(capacity));
                        }
                    }
                });
    }

    public void getCarModel(String seriesIds, String year, String capacity) {
        mRequestApi.listCarModel(HttpParams.getListCarModel(seriesIds, year, capacity))
                .compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribe(new ProgressSubscriber<ArrayList<CarModelBean>>() {
                    @Override
                    public void onNext(ArrayList<CarModelBean> carModelBeans) {
                        carmodels.clear();
                        for (CarModelBean bean : carModelBeans)
                            carmodels.add(new CarModelBeanViewModel(bean));
                        mAdapterBean.notifyDataSetChanged();
                    }
                });
    }

    public void showYear() {
        yearOrCapacity.clear();
        yearOrCapacity.addAll(years);
        mAdapterYear.notifyDataSetChanged();
    }

    public void showCapacity() {
        yearOrCapacity.clear();
        yearOrCapacity.addAll(capacitys);
        mAdapterYear.notifyDataSetChanged();
    }

    public ReplyCommand leftCommand = new ReplyCommand(() -> mFragment.getFragmentManager().popBackStack());
}
